
#ifndef SOCKET_TEST_SOCKETLIB_H
#define SOCKET_TEST_SOCKETLIB_H

#endif //SOCKET_TEST_SOCKETLIB_H

#include<iostream>
#include<cstdlib>



//head files of Linux
#include<netinet/in.h>
#include<unistd.h>   //for fork and read
#include<sys/types.h>   //for socket
#include<sys/socket.h>  //for socket
#include<string.h> // for bzero
#include<arpa/inet.h>

#include <fcntl.h>
#include <sys/select.h>
#include<sys/ioctl.h>
using namespace std;


struct robot_info
{
    char cmd1; // 检验位
    char cmd2; // 1-pose 2-angle
    char cmd3; // 1-left 2-right 3-双臂
    char cmd4; // 1-清除报警 2-初始化

    float l_angel1; // 度数0~180
    float l_angel2;
    float l_angel3;
    float l_angel4;
    float l_angel5;
    float l_angel6;
    float l_angel7;

    float l_x;
    float l_y;
    float l_z;
    float l_a;
    float l_b;
    float l_c;

    float r_angel1;
    float r_angel2;
    float r_angel3;
    float r_angel4;
    float r_angel5;
    float r_angel6;
    float r_angel7;

    float r_x;
    float r_y;
    float r_z;
    float r_a;
    float r_b;
    float r_c;

    float left_wheel;   // 速度 米/分钟  设置1m
    float right_wheel;  // 角速度 弧度/s 设置0.01rad
    float up_down;      // 固定位置 单位 mm 0-70mm

};




int socket_connect(const char* server_ip,const unsigned short server_port ){
    sockaddr_in myAddr;
    sockaddr_in remoteAddr;
    int sock, client_fd;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    // 创建
    if( sock == -1)
    {
        cerr<<"socket create fail!"<<endl;
        exit(1);
    }

    cout<<"sock :"<<sock<<endl;

    // 绑定
    myAddr.sin_family = AF_INET;
    myAddr.sin_port = htons(server_port);
    myAddr.sin_addr.s_addr = inet_addr(server_ip);

    // 建立连接
    while(connect(sock, (sockaddr*)&myAddr,sizeof(sockaddr)) == -1){
        sleep(1);
    }
    return sock;
}